//
//  ProjectIncomCostDetailViewController.swift
//  HuaYun
//
//  Created by enghou on 2019/1/16.
//  Copyright © 2019 xyxorigation. All rights reserved.
//

import UIKit

class ProjectIncomCostDetailViewController: IncomeCostListDetailViewController {

    
    var start = ""
    var end = ""
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func param(for name: String) -> Dictionary<String, Any> {
        return super.param(for: name) + ["orderType": self.orderType] + ["start": start,"end": end]
    }
    
    override func success(for name: String) -> NetSuccess {
        func suc(json: JSON)->Void{
            if let success = json.contents?["success"]?.bool {
                if success == true {
                    if let data = json.contents?["data"]?.contents {
                        var contents: [Dictionary<String,String?>] = []
                        contents.append(["统计计划编号:": data["serialnumber"]?.rawString()])
                        contents.append(["综合计划名称:": data["compositeName"]?.rawString()])
                        contents.append(["归属部门:": data["belongdeptone"]?.rawString()])
                        contents.append(["计划属性:": data["typeplanName"]?.rawString()])
                        
                        let income = (data["income"]?.int ?? 0) / 100
                        contents.append(["收入(元):": String(income).rmbformat])
                        let cost = (data["cost"]?.int ?? 0) / 100
                        contents.append(["成本(元):":String(cost).rmbformat])
                        let ml = (income - cost) > 0 ? income - cost : 0
                        contents.append(["毛利(元):": String(ml / 100).rmbformat])
                        if cost == 0 {
                            contents.append(["毛利率:": "0.00%"])
                        } else {
                            let mlrat = (Double(ml) / Double(cost)) * 100
                            let mlratstr = String(lround(mlrat)) + "%"
                            contents.append(["毛利率:": mlratstr])
                        }
                        let titlecontent = self.ctls[1] as! TitleContentTableViewController
                        titlecontent.source = contents
                        titlecontent.tableView.reloadData()
                        
                        let listProj = data["listProj"]?.array ?? []
                        let second = self.ctls[0] as! IncomeCostProjects
                        second.source = listProj
                        second.tableView.reloadData()
                    }
                } else {
                    self.alert(json.contents?["errorMsg"]?.rawString())
                }
                
            } else {
                self.alert(json.contents?["errorMsg"]?.rawString())
            }
        }
        return suc
    }
    
    override func path(for name: String) -> String {
        return ProjectIncomeCostDetail
    }
}
